import { _decorator, Component, EventTouch, Label, labelAssembler, log, Node, Sprite, SpriteFrame } from 'cc';
import { localConfig } from '../AshFramework/Utils/localConfig';
import { GameUtil } from './GameUtil';
import { GameManager } from './GameManager';
import { awardTime, AWARYTYPE } from '../AshFramework/Datas/GameConstant';
import { Timer } from './Timer';
import { MenuManager } from './MenuManager';
const { ccclass, property } = _decorator;

@ccclass('Shop')
export class Shop extends Component {

    @property(SpriteFrame)
    NormalSF: SpriteFrame = null;

    @property(SpriteFrame)
    CanelSF: SpriteFrame = null;

    @property(Sprite)
    AwardButtonSprite: Sprite = null;

    @property(Label)
    AwardCountLabel: Label = null;

    @property(Node)
    TimerNode: Node = null;

    @property(Sprite)
    TimerBtnSprite: Sprite = null;

    click_100(event: EventTouch) {
        if (GameManager.Instance.userData.AwardCount <= 0) {
            GameUtil.showHint("点击次数用完")
            return;
        }
        //领取砖石 --- 观看视频
        GameUtil.getAward(AWARYTYPE.MASONRY, MenuManager.Instance.getMasonryWorldPos(), () => {
            GameManager.Instance.userData.AwardCount--;
            this.showAwardCount();
            MenuManager.Instance.addMasonry(100);
        })
    }

    click_50(event: EventTouch) {
        if (!this.isCooling()) {
            GameUtil.showHint("冷却时间还没到")
            event.getCurrentTarget().getComponent(Sprite).spriteFrame = this.CanelSF;
            return;
        }
        //领取砖石
        //领取砖石 --- 观看视频
        GameUtil.getAward(AWARYTYPE.MASONRY, MenuManager.Instance.getMasonryWorldPos(), () => {
            GameManager.Instance.userData.timestampInMilliseconds = new Date().getTime();
            GameManager.Instance.saveData();
            this.TimerNode.getComponent(Timer).StartCounter(awardTime, () => {
                this.showTimer();
            });
            MenuManager.Instance.addMasonry(50);
            this.showTimer();
        })
    }

    click_25(event: EventTouch) {
        //领取砖石 --- 观看视频
        GameUtil.getAward(AWARYTYPE.MASONRY, MenuManager.Instance.getMasonryWorldPos(), () => {
            MenuManager.Instance.addMasonry(25);
        })
    }

    isCooling() {
        if (GameUtil.getSecondByTimestamp(GameManager.Instance.userData.timestampInMilliseconds) > awardTime) {
            return true;
        }
        return false;
    }

    showAwardCount() {
        if (GameManager.Instance.userData.AwardCount <= 0) {
            this.AwardButtonSprite.spriteFrame = this.CanelSF;
            this.AwardCountLabel.string = "0";
        } else {
            this.AwardButtonSprite.spriteFrame = this.NormalSF;
            this.AwardCountLabel.string = String(GameManager.Instance.userData.AwardCount);
        }
    }

    showTimer() {
        let second = GameUtil.getSecondByTimestamp(GameManager.Instance.userData.timestampInMilliseconds); // 间隔时间
        if (second >= awardTime) {
            this.TimerBtnSprite.spriteFrame = this.NormalSF;
            this.TimerNode.getComponent(Label).string = "领取";
        } else {
            this.TimerBtnSprite.spriteFrame = this.CanelSF;
            this.TimerNode.getComponent(Timer).StartCounter(awardTime - second, () => {
                this.showTimer();
            });
        }
    }

    protected onEnable(): void {
        this.showTimer();
        this.showAwardCount()
    }


}


